##parameters=assessmentName, model, user

## TODO: This should be in a factory class, not a skin method.
## TODO: Create a proper Exception class to return status failure messages


#get the sercurity, user provider and assessment storage services
securityService             = context.knowledgeengine_securityservice
assessmentStorageService    = context.knowledgeengine_assessmentstorageservice
creditManagementService     = context.knowledgeengine_creditmanagementservice

#------------------------------------#
# CREATE ASSESSMENT (in personal repository)

#get the CreateAssessmentsInPersonalRepository permission
createAssessmentsInPersonalRepositoryPermission = securityService.getEffectivePermission("CreateAssessmentsInPersonalRepository", model.getIdentity(), user.getIdentity())

#does the user have permission to create an assessment?
if createAssessmentsInPersonalRepositoryPermission is not None and createAssessmentsInPersonalRepositoryPermission.granted():
    #get the number of effective create credits remaining
    createAssessmentCredits = creditManagementService.getEffectiveCredits(createAssessmentsInPersonalRepositoryPermission, model, user)

    #are there any credits?
    if createAssessmentCredits != 0:
        #reduce the number of credits
        creditTxn = creditManagementService.reduceEffectiveCredits(createAssessmentsInPersonalRepositoryPermission, model, user, user.getIdentity())

        #did we have enough credits?
        if creditTxn is not None:
            #all ok... so create a new assessment
            assessment = assessmentStorageService.createAssessment(model)
            assessment.setName(assessmentName)
            assessment.setCreatedBy(user.getIdentity())
            assessment.setCreditTransactionIdentity(creditTxn)

            #save the assessment
            assessmentStorageService.saveAssessment(assessment)

            #add the assessment to the users repository
            assessmentStorageService.addAssessmentToRepository(assessment, user.getIdentity())

            #handle the 'onAfterCreateAssessmentInPersonalRepository' event for the model
            if model.isEventEnabled("onAfterCreateAssessmentInPersonalRepository"):
                model.getEnabledEvent("onAfterCreateAssessmentInPersonalRepository").execute(assessment, user)

                
            return assessment
                
        #there are unsaved credits... so we can't create an assessment
        else:
            #notify the user that there are no more assessment credits
            raise Exception, "* You have run out of assessment credits for %s.  Please contact your manager to acquire more credits." % (model.getTitleOrId(),)
            

    #no credits remaining... thus can't create an assessment                
    else:                
        #notify the user that there are no more assessment credits
        raise Exception, "You have run out of assessment credits for %s.  Please contact your manager to acquire more credits." % (model.getTitleOrId(),)

#user doesn't have permission to create an assessment
else:                
    #notify the user that they don't have permission to create an assessment
    raise Exception, "You do not have permission to create an assessment for %s.  Please contact your manager to request permission." % (model.getTitleOrId(),)
    


